﻿@* 购物车计数器 *@

@inject ICartService _cartService
@inject ISyncLocalStorageService _localStorage
@implements IDisposable


<a href="cart" class="btn btn-info">
    <i class="oi oi-cart"></i>
    <span class="badge">
        @GetCartItemsCount()
    </span>
</a>


@code {

    /// <summary>
    /// 获取购物车中商品数量
    /// </summary>
    /// <returns></returns>
    private int GetCartItemsCount()
    {
        var cart = _localStorage.GetItem<List<CartItem>>("cart");
        return cart != null ? cart.Count : 0;
    }


    protected override void OnInitialized()
    {
        _cartService.OnChange += StateHasChanged;
    }

    public void Dispose()
    {
        _cartService.OnChange -= StateHasChanged;
    }
}
